import Vue from 'vue'
// import Keycloak from 'keycloak-js'
import { ACCESS_TOKEN } from "@/store/mutation-types"
import store from '@/store'

// let initOptions = {
//   url: 'http://121.196.168.87/auth/', realm: 'zhongjia', clientId: 'vue-demo', onLoad: 'login-required'
// }

// let keycloak = Keycloak(initOptions);

/**
 * 单点登录
 */
const init = (callback) => {
  console.log("-------单点登录开始-------");
  
  // let token = Vue.ls.get(ACCESS_TOKEN);
  // if(token){
  //   loginSuccess(callback);
  // }else{
  //   validateSt( callback);
  // }

  validateSt( callback);

  console.log("-------单点登录结束-------");
};

const SSO = {
  init: init
};

function validateSt( callback) {

  store.dispatch('KeycloakLogin').then(res => {
    if(res)
      loginSuccess(callback);
    else
      console.log('====error====')
  }).catch((err) => {
    console.log(err);
    //that.requestFailed(err);
  });
}
function loginSuccess (callback) {
  callback();
}
export default SSO;